// pages/couponBagCenter/limitCouponDetail.js
const controller = require("../controllers/Controller").controller

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    accountCouponBagId: null,
    giveNum: null,
    maxGiveNum: null,
    isGive: false,
  },
  //用户持有券详情
  // getGoodsBagDetail() {
  //   controller.couponBagDetail({
  //     accountCouponBagId:this.data.accountCouponBagId
  //   }).then(res => {
  //     if(res.data.effectiveTimeState){
  //       res.data.effectiveEndTime = this.handleTime(res.data.effectiveEndTime)
  //       res.data.effectiveStartTime = this.handleTime(res.data.effectiveStartTime)
  //     }
  //     this.setData({
  //       detail: res.data
  //     })
  //   })
  // },

  //礼券包详情 2.0
  getBagActivityDetail() {
    let that = this
    controller.getActivityDetail({
      couponBagId: this.data.couponBagId
    }).then(res => {
      let list = []
      let data = res.data
      if (data.effectiveEndTime) {
        data.effectiveStartTime = data.effectiveStartTime.split(" ")[0].replace(/-/g, '.')
        data.effectiveEndTime = data.effectiveEndTime.split(" ")[0].replace(/-/g, '.')
      }
     
      this.setData({
        detail: data,
        type: data.type
      })
      if (res.data.couponMap) {
        this.setData({
          shopkey: Object.keys(res.data.couponMap)
        })
        this.data.shopkey.forEach(item => {
          for (let i = 0; i < res.data.couponMap[item].length; i++) {
            list.push(res.data.couponMap[item][i])
          }
        })
      }

      //list分组
      let fzLength = parseInt(list.length / 2)
      let fzList = []
      let num = 0
      for (let i = 0; i < fzLength; i++) {
        let itemList = []
        itemList.push(list[num])
        itemList.push(list[num + 1])
        num = num + 2
        fzList.push(itemList)
      }
      if (list.length % 2 != 0) {
        let lzList = []
        lzList.push(list[list.length - 1])
        fzList.push(lzList)
      }


      this.setData({
        shopDetail: fzList
      })
    })

    console.log("分享参数")
    console.log(this.data.invitationAccountId)
  },

  //获取店铺详情
  getShopDetail() {
    controller.shopDetails({
      shopId: this.data.shopId
    }).then(res => {
      this.setData({
        shopMsg: res.data
      })
    })
  },

  //跳转店铺详情
  toShopDetail(e) {
    let shopId = e.currentTarget.dataset.shopid
    wx.navigateTo({
      url: '/pages/Index/Index?shopId=' + shopId,
    })
  },
  phone(e) {
    let phone = e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },

  //跳转商品详情
  toGoodsDetail(e) {
    let shopId = e.currentTarget.dataset.shopid
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/Index/Detail?shopId=' + shopId + "&id=" + id,
    })
  },
  
  //  跳转播放视频页
  toVideoPage() {
    const data = {
      video: this.data.detail.video,
      videoDesc: this.data.detail.videoDesc || '',
      img: this.data.detail.img || this.data.detail.image ,
      name: this.data.detail.name ,

      type: this.data.type,
      couponBagId: this.data.couponBagId,

      isFromMyCenterVideo:true
    }
    wx.navigateTo({
      url: '/pages/TradingArea/videoPlay?data=' + JSON.stringify(data),
    })
  },

  //去使用
  toUse(e) {
    app.globalData.shopId = this.data.shopId
    wx.navigateTo({
      url: '/pages/Mall/Mall',
    })
  },
  //核销爆品
  writeOffGoods() {
    let value = {}
    value.accountCouponBagId = this.data.accountCouponBagId
    controller.HotBagQrCode(value).then(res => {
      if (res.data.qrCode) {
        wx.previewImage({
          urls: [res.data.qrCode],
        })
      }

    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      couponBagId: options.id,
      shopId: options.shopId,
      accountCouponBagId: options.accountCouponBagId
    })

    this.getBagActivityDetail()
    this.getShopDetail()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},


  /**
   * 用户点击右上角分享
   */
  async onShareAppMessage() {

    let accountCouponBagId = this.data.accountCouponBagId
    const giveImg = this.data.detail.giveImg || this.data.detail.img
    let value = {
      accountCouponBagId,
      num: 1
    }
    // 创建领取码
    const res = await controller.createGiveCode(value)
    const code = res.data
    // 私密转发
    wx.updateShareMenu({
      withShareTicket: true,
      isPrivateMessage: true,
      activityId: accountCouponBagId,
    })
    return {
      title: '好友赠送',
      path: '/pages/myCenter/receiveCoupon?accountCouponBagId=' + accountCouponBagId + '&code=' + code + '&num=1' + '&type=' + 4,
      imageUrl: giveImg,
    }
  }
})